package org.zhengquan.controller;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zhengquan.common.JsonData;
import org.zhengquan.dto.PageResult;
import org.zhengquan.service.SysRoleService;
import org.zhengquan.service.SysTreeService;
import org.zhengquan.service.SysUserService;
import org.zhengquan.vo.PageQuery;
import org.zhengquan.vo.UserVO;

import java.util.Map;

/**
 * @author zhengquan
 * @date 2017/10/31
 */
@Controller
@RequestMapping("/sys/user/")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysTreeService sysTreeService;

    @Autowired
    private SysRoleService sysRoleService;


    @RequestMapping("noAuth.page")
    public ModelAndView noAuth(){
        return new ModelAndView("noAuth");
    }


    @RequestMapping("save.json")
    @ResponseBody
    public JsonData save(UserVO userVO) {
        sysUserService.save(userVO);
        return JsonData.success();
    }

    @RequestMapping("update.json")
    @ResponseBody
    public JsonData update(UserVO userVO) {
        sysUserService.update(userVO);
        return JsonData.success();
    }

    @RequestMapping("acls.json")
    @ResponseBody
    public JsonData acls(Integer userId){
        Map<String,Object>map = Maps.newHashMap();
        map.put("acls",sysTreeService.userAclTree(userId));
        map.put("roles",sysRoleService.selectRoleListByUserId(userId));
        return JsonData.success(map);
    }

    @RequestMapping("page.json")
    @ResponseBody
    public JsonData page(@RequestParam Integer deptId, PageQuery pageQuery){
        PageResult result = sysUserService.selectPageByDeptId(deptId,pageQuery);
        return JsonData.success(result);
    }
}
